#include "departlist.h"
#include "ui_departlist.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlQueryModel>
#include <QTreeWidgetItem>
#include <QTreeWidget>
#include "cadreinfo.h"

DepartList::DepartList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DepartList)
{
    ui->setupUi(this);
}

DepartList::~DepartList()
{
    delete ui;
}
void DepartList::initTree(QString currentPerson)
{
    ui->treeWidget->clear();
    QSqlQueryModel *topmodel= new QSqlQueryModel();
    topmodel->setQuery("select unitid,unitname from unit where higherlevelunit=0 or higherlevelunit is null;");
    QHash<int,QString> tophash;
    ui->treeWidget->setColumnCount(1);
    QList<QTreeWidgetItem *> items;
    for(int i=0;i<topmodel->rowCount();i++){
        QString name=topmodel->record(i).value("unitname").toString();
        int id = topmodel->record(i).value("unitid").toInt();
        tophash[id]=name;
        items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("%1").arg(name))));
    }
    ui->treeWidget->insertTopLevelItems(0, items);
    QList<int> keys=tophash.keys();
    QString value;
    for(int i=0;i<keys.size();i++){
        value=tophash.value(keys[i]);
        int indexitem;
        for(int m=0;m<items.size();m++){
            if(items[m]->text(0)==value){
                indexitem=m;
            }
        }
        getCadre(items[indexitem],keys[i],currentPerson);
        getChild(items[indexitem],keys[i],currentPerson);
    }
}
// 树父节点及节点text对应的数据库的id
void DepartList::getChild(QTreeWidgetItem *item,int id,QString currentPerson)
{
    QSqlQueryModel *model= new QSqlQueryModel();
    QHash<int,QString> parenthash;
    QList<QTreeWidgetItem *> citems;
    model->setQuery(QString("select unitid,unitname from unit where higherlevelunit=%1;")
                      .arg(id));
    if(model->rowCount()>0){
        for(int j=0;j<model->rowCount();j++){
           int id = model->record(j).value("unitid").toInt();
           QString name=model->record(j).value("unitname").toString();
           parenthash[id]=name;
           citems<< new QTreeWidgetItem(item, QStringList(QString("%1").arg(name)));
        }
        QList<int> keys=parenthash.keys();
        QString value;
        for(int i=0;i<keys.size();i++){
            value=parenthash.value(keys[i]);
            int indexitem;
            for(int m=0;m<citems.size();m++){
                if(citems[m]->text(0)==value){
                    indexitem=m;
                }
            }
            getCadre(citems[indexitem],keys[i],currentPerson);
            getChild(citems[indexitem],keys[i]);
        }
    }
}
void DepartList::getCadre(QTreeWidgetItem *item,int id,QString currentperson)
{
    QSqlQueryModel *model= new QSqlQueryModel();
    model->setQuery(QString("select * from CadreInfo where UnitID=%1;")
                      .arg(id));
    QString name, idnum;
    for(int i=0;i<model->rowCount();i++){
        name = model->record(i).value("CadreName").toString();
        idnum = model->record(i).value("IDNum").toString();
        cadreHash[idnum]=name;
        QTreeWidgetItem * nitem=new QTreeWidgetItem(item, QStringList(QString("%1").arg(name)));
        if(!currentperson.isEmpty()&&idnum==currentperson){
            nitem->setExpanded(true);
            parentExpand(nitem);
            ui->treeWidget->setCurrentItem(nitem);
        }
    }
}

void DepartList::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    QString name = item->text(column);
    QString idnum = cadreHash.key(name);
    CadreInfo *cadreinfo = (CadreInfo*)this->parentWidget()->parentWidget()->parentWidget();
    QSqlQueryModel *model = cadreinfo->cadreModel;
    int row;
    for(int i=0;i<model->rowCount();i++){
       if(model->record(i).value("IDNum").toString()==idnum){
           row=i;
       }
    }
    int col=model->record().indexOf("IDNum");
    const QModelIndex index = model->index(row,col);
    cadreinfo->on_tableView_clicked(index);
}
void DepartList::parentExpand(QTreeWidgetItem *item)
{
    if (item->parent() != NULL) {
        QTreeWidgetItem *pItem = item->parent();
        if (! pItem->isExpanded()) {
            pItem->setExpanded(true);
        }
        parentExpand(pItem);
    }
}
